package timing.ukulele.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.portal.view.AntIconVO;
import timing.ukulele.facade.portal.mvc.AntIconControllerFacade;
import timing.ukulele.portal.convertor.IconConverter;
import timing.ukulele.portal.persistent.AntIcon;
import timing.ukulele.portal.service.AntIconService;
import timing.ukulele.web.util.Request2ModelUtil;

import java.util.List;

@RestController
public class AntIconController implements AntIconControllerFacade {
    private final AntIconService antIconService;

    @Autowired
    public AntIconController(AntIconService antIconService) {
        this.antIconService = antIconService;
    }

    @Override
    public ResponseData<AntIconVO> get(Long id) {
        if (id == null || id <= 0) {
            return ResponseData.paramError();
        }
        AntIcon po = this.antIconService.getById(id);
        if (po != null) {
            AntIconVO vo = IconConverter.INSTANCE.p2v(po);
            return ResponseData.success(vo);
        }
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<AntIconVO>> getByParam(HttpServletRequest request) {
        AntIcon antIcon = Request2ModelUtil.covert(AntIcon.class, request);
        List<AntIcon> poList = this.antIconService.list(new QueryWrapper<>(antIcon));
        if (CollectionUtils.isEmpty(poList)) {
            return ResponseData.success();
        }
        List<AntIconVO> voList = IconConverter.INSTANCE.p2vList(poList);
        return ResponseData.success(voList);
    }

    @Override
    public ResponseData<Boolean> add(AntIconVO sysAntIcon) {
        if (sysAntIcon == null || sysAntIcon.getId() != null) {
            return ResponseData.paramError();
        }
        AntIcon icon = IconConverter.INSTANCE.v2p(sysAntIcon);
        return ResponseData.success(this.antIconService.save(icon));
    }

    @Override
    public ResponseData<Boolean> delete(Long id) {
        if (id == null || id <= 0) {
            return ResponseData.paramError();
        }
        return ResponseData.success(this.antIconService.removeById(id));
    }

    @Override
    public ResponseData<Boolean> edit(AntIconVO sysAntIcon) {
        if (sysAntIcon == null || sysAntIcon.getId() == null) {
            return ResponseData.paramError();
        }
        AntIcon icon = IconConverter.INSTANCE.v2p(sysAntIcon);
        return ResponseData.success(this.antIconService.saveOrUpdate(icon));
    }

    @GetMapping("/page/{current}/{size}")
    public ResponseData<IPage<AntIconVO>> getPage(@PathVariable(name = "current") int current,
                                                  @PathVariable(name = "size") int size,
                                                  @RequestParam(value = "type", required = false) String type,
                                                  @RequestParam(value = "value", required = false) String value,
                                                  @RequestParam(value = "theme", required = false) String theme) {
        if (size == 0) {
            size = 10;
        }
        if (current == 0) {
            current = 1;
        }
        return ResponseData.success(this.antIconService.getPage(type, value, theme, current, size));
    }
}